<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>标题</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui/css/layui.css">
    <link rel="stylesheet" href="/static/css/weadmin.css">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<form class="layui-form searchForm">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">工区名称:</label>
            <div class="layui-input-inline">
                <input type="text" name="areaName" id="areaName" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <div class="layui-input-inline">
                <button class="layui-btn" lay-submit lay-filter="formDemo">查询</button>
                <button class="layui-btn" lay-submit>清空</button>
            </div>
        </div>
    </div>

</form>
<table class="layui-hide" id="dataList" lay-filter="dataList"></table>

<script type="text/html" id="toolbarOrg">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加工区</button>
        <button class="layui-btn layui-btn-sm" lay-event="del">批量删除</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>


    {{# if(d.parentId=='0'){ }}
    <a class="layui-btn layui-btn-xs" lay-event="addSubWorkArea">添加子级工区</a>
    <a class="layui-btn layui-btn-xs" lay-event="drawing">图纸管理</a>
    {{# }else{             }}
    {{# }                  }}
</script>

<script type="text/javascript" src="/lib/layui/layui.js"></script>
<script src="/static/js/main.js"></script>
<script>
    layui.use(['jquery', 'treetable','configureCom', 'configure', 'sessionFun', 'table'], function () {
        var $ = layui.jquery;
        var form=layui.form;
        var configure = layui.configure;
        var table = layui.table;
        var treetable = layui.treetable;
        var sessionFun = layui.sessionFun;
        var matchDict = layui.matchDict;
        var configureCom = layui.configureCom;
        var token = localStorage.getToken;
        var dataUrl = configure.PRJ_WORK_AREA_LIST_API + '?token=' + token;

        var cols = [[
            {type: 'numbers'},
            {field: 'areaName', width: 250,title: '工区名称'},
            {field: 'responsibleUserName', title: '责任人'},
            {field: 'responsibleUserMobile', title: '联系方式'},
            {title: '操作', toolbar: '#barDemo', width: 350}
        ]];

        window.renderTable = function (fields) {
            // 渲染表格
            layer.load(4);
            treetable.render({
                method: 'get',
                treeColIndex: 1,
                treeSpid: '0',
                treeIdName: 'areaId',
                treePidName: 'parentId',
                treeDefaultClose: true,
                treeLinkage: true,
                toolbar: '#toolbarOrg',
                elem: '#dataList',
                url: dataUrl,//baseApi + '/sys/menu/list?token=' + token,
                where: fields,
                //page: true,
                request: {
                    pageName: 'currPage', //页码的参数名称，默认：page
                    limitName: 'limit' //每页数据量的参数名，默认：limit
                },
                cols: cols,
                done: function () {
                    layer.closeAll('loading');
                }
            });
        };
        //初始化加载
        renderTable();

        //监听搜索
        form.on('submit(formDemo)', function (data) {
            var fields = data.field;
            renderTable(fields);
            return false;
        });



        /*
        var cols = [[
            {type: 'checkbox'},
            {title: '序号', type: 'numbers'},
            {field: 'areaName', title: '工区'},
            {field: 'reponsibleUserName', title: '责任人'},
            {field: 'contact', title: '联系方式'},
            {title: '操作', toolbar: '#barDemo', width: 250}
        ]];
        configureCom.tableRenderGet(configure.PRJ_WORK_AREA_LIST_API, {}, cols, 'dataList', 10, '', 'toolbarOrg');
        configureCom.tableSearch();
        */
        // 头工具栏事件
        table.on('toolbar(dataList)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            sessionFun.remove(configure.SESSION_LIST_DATA);
            switch (obj.event) {
                case 'add':
                    editItem('添加', configure.PRJ_WORK_AREA_SAVE_API, 0);
                    break;
                case 'del':
                    var data = checkStatus.data;
                    if (!data.length) {
                        top.layer.alert('请您至少选择一条数据后再提交！');
                        return false;
                    }
                    top.layer.confirm('您真的要删除选中的数据吗？', function (index, layero) {
                        top.layer.close(index);
                        var ids = '';
                        $(data).each(function (index, item) {
                            if (!index) {
                                ids = item.areaId;
                            } else {
                                ids += ',' + item.areaId;
                            }
                        });
                        deleteFun(ids);
                    });
                    break;
            }
        });

        // 每行操作
        table.on('tool(dataList)', function (obj) {
            var data = obj.data;
            console.log(data)
            sessionFun.set(configure.SESSION_LIST_DATA, data);
            if (obj.event === 'del') {//查看详情
                deleteFun(data.areaId);
            } else if (obj.event === 'edit') {
                if(data.parentId=="0"){
                    editItem('编辑', configure.PRJ_WORK_AREA_SAVE_API, 0);
                }else{
                    editItem('编辑', configure.PRJ_WORK_AREA_SAVE_API, 1);
                }
            } else if (obj.event === 'drawing') {
                top.layer.open({
                    type: 2,
                    title: '图纸管理',
                    content: configureCom.nocache(configure.PRJ_WORK_AREA_DRAWING_HTML+'?areaId='+data.areaId),
                    area: configureCom.Psize1,
                    cancel: function (index, layero) {
                    }
                });
            } else if(obj.event==='addSubWorkArea'){
                editItem('创建子级工区', configure.PRJ_WORK_AREA_SAVE_API, 1)
            }
        });


        //（批量）删除工区
        function deleteFun(areaIds) {
            layer.confirm('确定删除此工区？',{icon:3, title:'提示信息'},function(index){
                var data = {};
                data.areaIds = areaIds;
                var index2 = top.layer.load(1, {shade: 0.01});
                $.ajax({
                    url: configure.PRJ_WORK_AREA_REMOVE_API+'/'+areaIds+'?token='+token,
                    type: 'delete',
                    cache: false,
                    data: data,
                    dataType: 'json',
                    success: function (data) {
                        top.layer.close(index2);
                        configureCom.errFun(data);
                        if (data.code == 0) {
                            top.layer.msg('删除成功！');
                            treeTableRefresh();
                        } else {
                            return false;
                        }
                    },
                    error: function (data) {
                        top.layer.close(index2);
                        top.layer.alert('删除失败！');
                    }
                });
                layer.close(index);
            });

//            configureCom.deleteAjax(configure.PRJ_WORK_AREA_REMOVE_API+'?token='+token, {"areaIds": areaIds}).then(function (res) {
//                debugger;
//                if (res.code == 0) {
//                    top.layer.msg('删除成功！');
//                    treeTableRefresh();
//                } else {
//                    top.layer.alert('删除失败！');
//                }
//            });
        }

        /**
         * 修改(创建)工区
         * @param title
         * @param url
         * @param saveSubWorkArea 1-子工区 0-一级工区
         */
        function editItem(title, url, saveSubWorkArea) {
//            var saveSubWorkArea = title=='创建子级工区'?1:0;
            top.layer.open({
                type: 2,
                title: title,
                area: configureCom.Psize1,
                content: configureCom.nocache(''+configure.PRJ_WORK_AREA_EDIT_HTML+'?saveSubWorkArea='+saveSubWorkArea),
                btn: ['保存'],
                yes: function (index, layero) {
                    var iframeWin = top.window.frames['layui-layer-iframe' + index]; //得到iframe页的窗口对
                    var data = iframeWin.getData();
                    console.log(data);
                    configureCom.postDataAjax(url, data).then(function (res) {
                        if (res.code == 0) {
                            top.layer.close(index);
                            top.layer.msg('保存成功！');
                            treeTableRefresh();
                        } else {
                            top.layer.alert('保存失败！');
                        }
                    });
                }
            });
        }

        function treeTableRefresh(){
            var areaName = $("#areaName").val();
            var obj  = new Object();
            obj.areaName = areaName;
            renderTable(obj);
            return false;
        }
    });

</script>
</body>
</html>